/*
 * FileName：InitWebMagicListener.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.webmagic.listener;

import com.smart.quartz.entity.JobEntity;
import com.smart.quartz.job.SohuNewsJob;
import com.smart.quartz.service.IJobService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 *
 * @author hermit
 * @version 2.0
 * @date 2018-04-17 10:54:58
 */
@Component
public class InitWebMagicListener implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(SohuNewsJob.class);

    @Autowired
    private IJobService jobService;

    @Autowired
    private Scheduler scheduler;

    @Override
    public void run(String... args) throws Exception {
        List<JobEntity> jobEntities = jobService.quertListJobByPage(new JobEntity());
        //新增
        if(CollectionUtils.isEmpty(jobEntities)){
            logger.info("爬取搜狐热门新闻");
            JobEntity quartz = new JobEntity();
            quartz.setJobName("souhuSelectedNewsJob");
            quartz.setJobGroup("smartwx");
            quartz.setJobDesc("爬取搜狐热门新闻");
            quartz.setJobClassName("com.smart.webmagic.job.SouhuSelectedNewsJob");
            quartz.setJobCron("0 30 5 * * ?");
            Class cls = Class.forName(quartz.getJobClassName()) ;
            cls.newInstance();
            //构建job信息
            JobDetail job = JobBuilder.newJob(cls).withIdentity(quartz.getJobName(),
                    quartz.getJobGroup())
                    .withDescription(quartz.getJobDesc()).build();
            //添加JobDataMap数据
            job.getJobDataMap().put("title", "SmartWx是一款基于JAVA企业级平台研发的微信公众号管理系统， 依托企业级JAVA的高效、安全、稳定等优势，开创国内JAVA版开源微信公众号管理系统先河！");
            job.getJobDataMap().put("url", "https://www.smartwx.info/");
            // 触发时间点
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getJobCron());
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(quartz.getJobName(), quartz.getJobGroup())
                    .startNow().withSchedule(cronScheduleBuilder).build();
            //将该给定添加JobDetail到调度程序，并将给定Trigger与它关联
            scheduler.scheduleJob(job, trigger);
        }
    }
}
